/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

var btconfig = {
    rowStyle: {classes: 'list', height: 20},//定义列的class
    iconsPrefix: "icon",
    sortName: 'id',
    classes: 'table table-condensed table-hover',
    striped: true,
    cache: false,
    method: 'post',
    contentType: 'application/x-www-form-urlencoded',
    pagination: true,
    queryParamsType: 'page',
    sidePagination: "server",
    smartDisplay: false,
    showFooter: true,
    showPaginationSwitch: true,
    showRefresh: true,
    showToggle: true,
    showColumns: true,
    minimumCountColumns: 1,
    clickToSelect: true,
    detailView: true,
    cardView: false,
    pageList: [10, 25, 50, 100],
    responseHandler: function (res) {
        var page = {};
        page.total = res.count;
        if(res.list){
            page.rows = res.list;
        }else{
            page.rows = [];
        }
        return page;
    },
    userQuery:function(){
        return {};
    },
    queryParams: function (params) {
        params.pageNo = params.pageNumber;
        if(params.sortName){
            params.sortName = params.sortName.replace(/[A-Z]/g,function(code){
                return "_"+code;
            });
            params.sortName = params.sortName.toUpperCase();
            params.orderBy = params.sortName + " " + params.sortOrder;
        }
        var userParms = this.userQuery();
        $.extend(params,userParms);
        return params;
    },
    detailFormatter: function (index, row) {
        return JSON.stringify(row);
    },
    icons: {
        paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
        paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
        refresh: 'glyphicon-refresh icon-refresh',
        toggle: 'glyphicon-list-alt icon-list-alt',
        columns: 'glyphicon-th icon-th',
        detailOpen: 'glyphicon-plus icon-plus',
        detailClose: 'glyphicon-minus icon-minus'
    }
};

$.fn.BTConfig = function(object){
    return $.extend({},btconfig,object);
}

$.fn.refreshByForm = function(url){
    var _form = $(this);
    var _tagName = _form[0].tagName;
    if(_tagName && _tagName.toUpperCase() == "FORM"){
        var formValues = _form.serializeArray();
        var request={};
        var urlRequest = {url:url};
        $.each(formValues,function(){
            if(request[this.name]){
                var val = request[this.name];
                if(typeof(val)=="string"){
                    request[this.name] = [];
                    request[this.name].push(val);
                }
                request[this.name].push(this.value);
            }else{
                request[this.name] = this.value;
            }
        });
        if(!url){
            urlRequest = {url:_form.attr("action")};
        }
        request = $.extend(urlRequest,{query:request});
        return request;
    }else{
        throw "this is not form";
    }
}

$.fn.BTConfigSimple = function(object){
    var simple = $.extend({},btconfig,{
        clickToSelect: false,
        showRefresh: false,
        showToggle: false,
        showColumns: false,
        detailView: false,
        showPaginationSwitch: false,
        showFooter: false
    });
    return $.extend({},simple,object);
}




